package com.tjl.yangxixis.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lyp.activitys.CarYuyueTimeActivity;
import com.lyp.activitys.DectionYuyueTimeActivity;
import com.lyp.activitys.IndoorYuyueTimeActivity;
import com.lyp.adapters.YuyueTimeAdapter;
import com.lyp.jsonbean.LoginBean;
import com.lyp.jsonbean.YuyueTimeBean;
import com.lyp.yangxixi.photo.utils.Popwindows;
import com.lyp.yangxixi.photo.utils.WindowBackgroundAlphaUtils;
import com.tjl.yangxixis.R;
import com.tjl.yangxixis.api.YangxixiApi;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 *
 * @author Administrator
 *  预约列表
 */
public class OrderListActivity extends Activity implements OnClickListener{

	private ImageView mOrderlist,mBack;
	private Popwindows myWindow;
	private RecyclerView mRecyclerView;
	private String date;
	private LoginBean.DataBean bean;
	private YuyueTimeAdapter mAdapter;
	private LinearLayoutManager mLinearLayoutManager;
	private List<YuyueTimeBean.DataBean> mLists = new ArrayList<>();
	private TextView mCounts;

	private int pages;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_order_list);
		initview();
	}


	void initview(){
		bean = (LoginBean.DataBean) getIntent().getExtras().get("databean");
		mCounts = (TextView) findViewById(R.id.yuyue_size);
		mOrderlist = (ImageView) findViewById(R.id.iv_orderlistbutton);
		mBack = (ImageView) findViewById(R.id.iv_orderback);
		mRecyclerView= (RecyclerView) findViewById(R.id.listview);
		mLinearLayoutManager = new LinearLayoutManager(this);
		mRecyclerView.setLayoutManager(mLinearLayoutManager);
		mAdapter = new YuyueTimeAdapter(mLists);
		mRecyclerView.setAdapter(mAdapter);
		mAdapter.setOnItemClickListener(new YuyueTimeAdapter.MyItemClickListener() {
			@Override
			public void onItemClick(View v, int position) {
				if(bean.getServer_select().equals("车内空气净化服务")){
					Intent intentcar = new Intent(OrderListActivity.this,CarYuyueTimeActivity.class);
					Bundle bundle = new Bundle();
					bundle.putSerializable("databean",mLists.get(position));
					LogUtils.i("tag",bean.getId());
					intentcar.putExtras(bundle);
					startActivity(intentcar);
				}
				else if(bean.getServer_select().equals("第三方检测服务")){
					Intent intentdection = new Intent(OrderListActivity.this,DectionYuyueTimeActivity.class);
					Bundle bundle = new Bundle();
					bundle.putSerializable("databean",mLists.get(position));
					LogUtils.i("tag",bean.getId());
					intentdection.putExtras(bundle);
					startActivity(intentdection);
				}
				else if(bean.getServer_select().equals("室内空气净化服务")){
					Intent intentindoor = new Intent(OrderListActivity.this,IndoorYuyueTimeActivity.class);
					Bundle bundle = new Bundle();
					bundle.putSerializable("databean",mLists.get(position));
					intentindoor.putExtras(bundle);
					startActivity(intentindoor);
				}
			}
		});

		mBack.setOnClickListener(this);
		mOrderlist.setOnClickListener(this);
	}

	public void getText(String date){
		yuyue(bean.getId(), date, pages);

	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		Intent intent;
		switch (arg0.getId()) {
			//弹出日期界面
			case R.id.iv_orderlistbutton:
				getSelectPic();
				break;
			case R.id.iv_orderback:
				 finish();
				break;
		}
	}


	private void getSelectPic() {
		myWindow = new Popwindows(this,null);
		myWindow.showAtLocation(mOrderlist, Gravity.CENTER, 0, 0);//只显示在相对父控件ll_parent中间位置，如果是底部，那就加上Gravity.BOTTOM
		WindowBackgroundAlphaUtils.backgroundAlpha(OrderListActivity.this, 0.2f);
		myWindow.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss() {
				// TODO Auto-generated method stub
				date = myWindow.text();
				getText(date);
				//设置popupWindow监听，当消失的时候，记得把背景变亮。

				WindowBackgroundAlphaUtils.backgroundAlpha(OrderListActivity.this, 1.0f);
			}
		});
	}




	//预约时间查询
	public void yuyue(String a_id,String subscriber_day,int page){
		Retrofit retrofit = new Retrofit.Builder()
				.baseUrl(YangxixiApi.APP_URL)
				.addConverterFactory(GsonConverterFactory.create())
				.build();

		YangxixiApi api = retrofit.create(YangxixiApi.class);
		Call<YuyueTimeBean> call = api.getYuyueTime(a_id,subscriber_day,page);
		call.enqueue(new Callback<YuyueTimeBean>() {
			@Override
			public void onResponse(Call<YuyueTimeBean> call, final Response<YuyueTimeBean> response) {
				mCounts.setText(response.body().getCounts()+"");
				if (response.body().getResult() == 1 ) {
					OrderListActivity.this.runOnUiThread(new Runnable() {
						@Override
						public void run() {
							mLists.clear();
							mLists.addAll(response.body().getData());
							mAdapter.notifyDataSetChanged();
						}
					});
				} else {
					mLists.clear();
					mAdapter.notifyDataSetChanged();
				}
			}
			@Override
			public void onFailure(Call<YuyueTimeBean> call, Throwable t) {
				ToastUtils.showShort(t.getMessage());
			}
		});
	}

}
